/* 
 * Parasyti program, kuri leidzia vartotojui pasirinkti (grafiniame 
 * lange) tekstini fail bei parodo jo turini (tekst) grafiniame ekrane (Swing).
 *
 *
 * Hints: 
 * javax.swing.Window, javax.swing.JTextArea, java.io.FileInputStream, 
 * javax.swing.JFileChooser To change this template, choose Tools | Template Manager and 
 * open the template in the editor. .
 *
 *                       Tatjana Aerchvo
 *                      II kursas 3 grupe
 *
 */ 

import java.awt.BorderLayout; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 

import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

// PUBLIK ,     .
//EXTENDS.Cto by nasledovat klass, nuzno prosto vkliucit opredelenie odnogo klassa v drugoe.
public class Main extends JFrame { 
//PRIVATE ciastnyj dostup
    private JPanel jContentPane = null; 
    private JButton jButton = null; 
    private JScrollPane jScrollPane = null; 
    private JTextArea jTextArea = null; 
    //  Konstruktor klassa Main.(inicializiruet objekt posle ego sozdanija). 
   
    
    public Main() { 
       //SUPER vsegda obrasciaetsia k superklassu vyzyvajuscego klassa
        super(); 
        initialize(); 
    } 



//VOID.      ,   ,    .    
//   void
    private void initialize() { 
        //  Nurodome dydzius ir vieta. 
        this.setSize(482, 354); 
        //  Duodam konteineri komponentams. 
        this.setContentPane(getJContentPane()); 
        this.setTitle("Tatjanos Arechvo 1 programa"); 
    } 
    
    
    
    private JPanel getJContentPane() { 
        if (jContentPane == null) { 
            jContentPane = new JPanel(); 
            jContentPane.setLayout(new BorderLayout()); 
            jContentPane.add(getJButton(), BorderLayout.NORTH); 
            jContentPane.add(getJScrollPane(), BorderLayout.CENTER); 
        } 
        return jContentPane; 
    } 

    private JButton getJButton() { 
        if (jButton == null) { 
            jButton = new JButton(); 
            jButton.setText("Pasirinkite faila"); 
            jButton.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) { 
                    JFileChooser chooser = new JFileChooser(); 
                    chooser.setAcceptAllFileFilterUsed(true); 
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
                    chooser.setMultiSelectionEnabled(false); 
                    if (  chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION ) { 
                        // Loading file content 
                        try { 
                            File file = chooser.getSelectedFile(); 
                            BufferedReader reader = new BufferedReader(new FileReader(file)); 
                            StringBuffer content = new StringBuffer(); 
                            String line = null; 
                            while ( ( line = reader.readLine() ) != null) { 
                                content.append(line).append("\n"); 
                            } 
                            getJTextArea().setText(content.toString()); 
                            reader.close(); 
                        } catch ( Exception ex ) { 
                            JOptionPane.showMessageDialog(null, "An error occured while reading file content!"); 
                        } 
                    } 
                } 
            }); 
        } 
        return jButton; 
    } 

    private JScrollPane getJScrollPane() { 
        if (jScrollPane == null) { 
            jScrollPane = new JScrollPane(); 
            jScrollPane.setViewportView(getJTextArea()); 
        } 
        return jScrollPane; 
    } 

    private JTextArea getJTextArea() { 
        if (jTextArea == null) { 
            jTextArea = new JTextArea(); 
        } 
        return jTextArea; 
    } 

    // static     main ()     
    //      ,   ,    . 
    //       void
    public static void main(String[] args) { 
        new Main().setVisible(true); 
    } 
}